//第一步,引入vue和vuex
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

//第二步,创建一个store
let store=new Vuex.Store({
	data(){

	},
	state:{
		//存放数据
		carPanelData:[],
        //是否为最大值
        maxOff:false,

        //控制隐藏显示
        carShow:false,
        //创建小球
        ball:{
        	show:false, //显示与隐藏
        	el:null, //点击了哪一个
        	img:''
        },
        //地址
        receiveInfo:[{
            "name": "王某某",
            "phone": "13811111111",
            "areaCode": "010",
            "landLine": "64627856",
            "provinceId": 110000,
            "province": "北京市",
            "cityId": 110100,
            "city": "市辖区",
            "countyId": 110106,
            "county": "海淀区",
            "add": "上地十街辉煌国际西6号楼319室",
            "default": false
            },
            {
            "name": "李某某",
            "phone": "13811111111",
            "areaCode": "010",
            "landLine": "64627856",
            "provinceId": 110000,
            "province": "北京市",
            "cityId": 110100,
            "city": "市辖区",
            "countyId": 110106,
            "county": "海淀区",
            "add": "上地十街辉煌国际东6号楼350室",
            "default": true
        }],
        // 储存购买商品的订单信息
        orderData:[]
	},
	// 价格数量的计算
	getters:{
		// 计算数量
        totleCount(state){            
             let count=0;
             state.carPanelData.forEach(goods=>{
             	count += goods.count
             })
             return count
        },
        //计算价格
        totlePrice(state){
             let price=0;
             state.carPanelData.forEach(goods=>{
             	price += goods.count * goods.price
             })
             return price
        },
        //全选
        allChecked(state){
        	let allcheck=true
        	state.carPanelData.forEach(goods=>{
        		if(!goods.check){
                    allcheck = false
                    return
        		}
        	})
        	return allcheck
        },
        //计算总金额
        checkedPrice(state){
            let price=0
            state.carPanelData.forEach(goods=>{
                if(goods.check){
                    price+=goods.price*goods.count
                }
            })
            return price
        },
        //计算数量
        checkedCount(state){
            let count=0
            state.carPanelData.forEach(goods=>{
                if(goods.check){
                    count+=goods.count
                }
            })
            return count
        },
        //当前加入的商品
        checkGoods(state){
            let checkGoods=[]
            state.carPanelData.forEach(goods=>{
                if(goods.check){
                    checkGoods.push(goods)
                }
            })
            return checkGoods
        }
	},
	mutations:{
		// 接收数据  添加商品
		addCarpanelData(state,data){
			// bOff 是判断购物车是否添加过
            let bOff=true  //(为true 不用增添商品,直接添加数量即可)
             //为false  添加 商品
			//goods 购物车存放的数据
			state.carPanelData.forEach(goods=>{
				// data.sku_id 加入购物车传进来的数据
				if(goods.sku_id==data.info.sku_id){
					goods.count += data.count
					if(goods.count > goods.limit_num){
						goods.count -= data.count
						state.maxOff=true
						bOff = false
						return
					}
					bOff = false

					state.carShow=true
					//更改小球的状态
					state.ball.show=true
					state.ball.img=data.info.ali_image
					state.ball.el=event.path[0]  //设置位置
				}
			})


		    if(bOff){ 
        		state.carShow=true

		      //为true  添加数量
		    	//存放数据
				let goodsData=data.info
				       // 对象    变量   数量
				Vue.set(goodsData,'count',data.count)
				           // 购物车中产品默认是选择的转态
				Vue.set(goodsData,'check',true)

				state.carPanelData.push(goodsData)
               // 小球动画效果状态
				state.ball.show=true
				state.ball.img=data.info.ali_image
	            state.ball.el=event.path[0]

		    }
		},
		// 删除购物车产品
		delCarPanelData (state,id) {
	  	  state.carPanelData.forEach((goods,index) => {
	        if(goods.sku_id === id){
	          state.carPanelData.splice(index,1)
	          return
	        }
	      })
	  	},
		//数量超额 关闭按钮
		closePrompt(state){
			state.maxOff = false
		},
		//显示购物车
		showCar(state){
			state.carShow=true
		},
		// 隐藏购物车
		hideCar(state){
			setTimeout(()=>{
				state.carShow=false
			},500)
		},
        // 购物车上产品增加
        plusCartPanelData(state,id){
            state.carPanelData.forEach((goods,index)=>{
            	if(goods.sku_id===id){
                    if(goods.count>=goods.limit_num) return
                    goods.count ++
                    return 
            	}

            })
        },

        // 购物车上产品减少
        subCartPanelData(state,id){
            state.carPanelData.forEach((goods,index)=>{
            	if(goods.sku_id===id){
                    if(goods.count<=1) return
                    goods.count --
                    return 
            	}

            })
        },
        // 购物车上产品选中与不选中
        checkGoods(state,id){
            state.carPanelData.forEach((goods,index)=>{
            	if(goods.sku_id===id){
                    goods.check=!goods.check
                    return 
            	}

            })
        },
        //控制全选
        allCheckGoods(state,allChecked){
            state.carPanelData.forEach((goods,index)=>{
                // goods.check=!goods.check
            	goods.check=!allChecked
            })
        },
        //删除选中的商品
        delCheckGoods(state){
            // 方法一:向前删除
            // state.carPanelData.forEach((goods,index)=>{
            //     if(goods.check){
            //         state.carPanelData.splice(index,1)
            //     }
            // })
            //方法二:从尾部开始删除
            let i=state.carPanelData.length            
            while(i--){
                if(state.carPanelData[i].check){
                    state.carPanelData.splice(i,1)
                }
            }
        },
        //添加收货人信息
        submitInfo22(state,data){
            if(data[0].default){
                state.receiveInfo.forEach((receive,index)=>{
                    receive.default=false
                    receive.checked=false
                })
            }
            if(data[1]==null){
                state.receiveInfo.push(data[0])
            }else{
                console.log(data[0])
                state.receiveInfo[data[1]] = data[0]
            }
        },
        //提交订单
        submitOrder(state,data){
            state.orderData.unshift(data)
            //删除订单上的信息
            let i=state.carPanelData.length
            while(i--){
                if(state.carPanelData[i].check){
                    state.carPanelData.splice(i,1)
                }
            }
        },
        // 现在支付成功
        payNow(state,id){
            state.orderData.forEach((order)=>{
                //判断id一样  就显示已完成 已付款
                if(order.orderId===id){
                    order.isPay=true
                    return
                }
            })
        }
        
	}
})

//第三步,将store暴露
export default store